//
//  AppDelegate.m
//  DebugDemo
//
//  Created by XFB on 2018/11/8.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "AppDelegate.h"
#import "ZCTabBarController.h"
#import "Person.h"

#import "StarViewController.h"
#import "Data.h"

#import "ComplaintModel.h"

@interface AppDelegate ()

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation AppDelegate

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Complaint.plist" ofType:nil];
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:plistPath];
        NSArray *dataArray = [ComplaintModel mj_objectArrayWithKeyValuesArray:plistArray];
        _dataSource = [dataArray mutableCopy];
    }
    return _dataSource;
}

- (void)sortDataArray {
    
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    NSLog(@"1------%@", timeString);
    
    // 添加时间戳
    NSMutableArray *timeArray = @[].mutableCopy;
    // 时间戳key complaintM是value
    NSMutableDictionary *dateKeyDict = [[NSMutableDictionary alloc] init];
    
    for (ComplaintModel *complaintM in self.dataSource) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [formatter dateFromString:complaintM.time];
        // 添加时间
        [timeArray addObject:date];
        // 日期转时间戳
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
        NSLog(@"timeSp:%@",timeSp);
        [dateKeyDict setObject:complaintM forKey:timeSp];
    }

    NSLog(@"%@", dateKeyDict);
    
    /// 排序
    NSArray *orderedDateArray = [timeArray sortedArrayUsingComparator:^NSComparisonResult(NSDate  *_Nonnull obj1, NSDate *_Nonnull obj2) {
        return [obj2 compare:obj1];
    }];
    
    NSLog(@"%@", orderedDateArray);
    NSMutableArray *sortedArray = @[].mutableCopy;
    NSDate *dateKey = [[NSDate alloc] init];
    for (NSInteger i = 0; i < orderedDateArray.count; i ++) {
        dateKey = orderedDateArray[i];
        // 日期转换成时间戳字符串key
        NSString *timeSp = [NSString stringWithFormat:@"%lld", (long long)[dateKey timeIntervalSince1970]];
        NSLog(@"%@", timeSp);
        // 根据时间戳字符串key取对应的对象
        [sortedArray addObject:[dateKeyDict objectForKey:timeSp]];
    }
    
    _dataSource = sortedArray;
    NSLog(@"%@", sortedArray);
    
    NSDate* date1 = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a1=[date1 timeIntervalSince1970]*1000; // *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString1 = [NSString stringWithFormat:@"%.0f", a1]; //转为字符型
    NSLog(@"2------%@", timeString1);

}

/**
 
Wed Nov 20 12:12:23 2019        1574223143
Fri Nov 15 10:13:19 2019        1573783999
Tue Nov 19 09:23:10 2019        1574126590
Mon Nov 18 18:29:45 2019        1574072985
Tue Nov 12 04:45:34 2019        1573505134
Wed Nov 20 09:36:23 2019        1574213783
Tue Nov 19 08:55:12 2019        1574124912
Sat Nov 16 15:22:18 2019        1573888938
Sun Nov 17 23:29:12 2019        1574004552
 
 */



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self sortDataArray];
    
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
//    NSMutableArray *plistArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
//    NSArray *modelArray = [Data mj_objectArrayWithKeyValuesArray:plistArray];
//
//    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
//    NSInteger order = 0;
//
//    for (Data *dataModel in modelArray) {
//        NSString *GRPNBR = dataModel.GRPNBR;
//        NSString *RMBBAL = dataModel.RMBBAL;
//        NSMutableDictionary *keyDict = [[resultDict objectForKey:GRPNBR] mutableCopy];
//        if (!keyDict) {
//            keyDict = [resultDict mutableCopy];
//        }
//
//    }
   
    /// 之后在排序
    NSObject *obj = [[NSObject alloc] init];
//    /// 获得NSObject 实例对象的成员变量 所占用的大小 >> 8
//    NSLog(@"%zd", class_getInstanceSize([NSObject class]));
//    // 获得obj 指针所指向内存的大小 >> 16
//    NSLog(@"%zd", malloc_size((__bridge const void *)obj));
    
//    NSString *string = @"123456";
//    NSArray *arr = [[NSArray alloc] initWithObjects:string, nil];
//    NSMutableArray *array = @[].mutableCopy;
//    for (NSUInteger string in arr.count) {
//        NSLog(@"%@", string);
//    }
    
//    NSMutableArray *array = [NSMutableArray arrayWithObject:@"1"];
//    [array removeObjectsInRange:NSMakeRange(0, 2)];
//    [array removeObjectAtIndex:2];
    
    NSMutableArray<NSNumber *> *testData = [NSMutableArray array];
    
    [testData addObject:@(-1)];
    [testData addObject:@(0)];
    [testData addObject:@(1)];
    [testData addObject:@(2)];
    [testData addObject:@(-1)];
    [testData addObject:@(-4)];
    
    NSMutableDictionary<NSNumber *, NSNumber *> *recMdict = [NSMutableDictionary dictionary];
    for (NSNumber *n in testData) {
        [recMdict setObject:@(recMdict[n].integerValue + 1) forKey:n];
    }
    
    [testData removeAllObjects];
    for (NSNumber *key in recMdict.allKeys) {
        NSLog(@"%@", key);
        NSLog(@"---------   %@", recMdict[key]);
        NSInteger count = recMdict[key].integerValue;
        if (key.integerValue == 0) {
            count = count > 3 ? 3 : count;
        } else {
            count = count > 2 ? 2 : count;
        }
        for (NSInteger i = 0; i < count; i++) {
            [testData addObject:key];
        }
    }
    
    NSArray<NSNumber *> *sortArr = [testData sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        
        NSLog(@"%@---%@", obj1, obj2);
        return [obj1 compare:obj2];
    }];

    NSMutableArray<NSArray<NSNumber *>*> *result = [NSMutableArray array];
    NSInteger arrCount = sortArr.count;
    for (NSInteger i = 0; i < arrCount - 2; i++) {
        NSInteger j = i + 1;
        NSInteger k =  arrCount - 1;
        while (k > j) {
            NSInteger sum = sortArr[i].integerValue + sortArr[j].integerValue + sortArr[k].integerValue;
            if (sum == 0) {
                [result addObject:@[sortArr[i], sortArr[j], sortArr[k]]];
                j++;
                k--;
            } else if (sum > 0) {
                k--;
            } else {
                j++;
            }
        }
    }
    
    NSLog(@"");
    
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    for (NSArray<NSNumber *> *arr in result) {
        NSMutableString *mStr = [NSMutableString string];
        NSArray<NSNumber *> *result1 = [arr sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
            return [obj1 compare:obj2];
        }];
        for (NSNumber *n in result1) {
            [mStr appendString:n.stringValue];
        }
        [mDict setObject:arr forKey:mStr];
    }
    
    
    NSLog(@"");
    NSLog(@"%@", mDict);
    
    
    
    
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.rootViewController = [[ZCTabBarController alloc] init];
    [self.window makeKeyAndVisible];
    
    [self add3DTouchItems:application];
    
    return YES;
}

- (void)add3DTouchItems:(UIApplication *)application{

    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome];
    UIMutableApplicationShortcutItem *publishComplaintItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"publishComplaint" localizedTitle:@"拍照"];
    publishComplaintItem.icon = icon1;
    
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    UIMutableApplicationShortcutItem *appShareItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"share" localizedTitle:@"分享"];
    appShareItem.icon = icon2;
    
    //绑定到App icon
    application.shortcutItems = @[publishComplaintItem, appShareItem];
}

//UIApplicationDelegate 在这里处理点击的item
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
    if ([shortcutItem.type isEqualToString:@"share"]) {
        
    } else if ([shortcutItem.type isEqualToString:@"publishComplaint"]) {

    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
